-- 这是Main
--[[
作者: @破晓_年华
原帖: http://tieba.baidu.com/p/4507852884
--]]

displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
-- Use this function to perform your initial setup
function setup()
rectMode(CENTER)
print("Hello World!")
Class={StartMenu(),TwoPlayer(),OnePlayer()}
Cc=1
Hard=0
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color 
background(62, 153, 26, 255)
Class[Cc]:draw()
-- This sets the line thickness
strokeWidth(5)

-- Do your drawing here

end

function touched(touch)
Class[Cc]:touched(touch)
end

function CreatBall(x,y,r)
local body=physics.body(CIRCLE,r)
body.x=x
body.y=y
return body
end

CreatPlayer=function(x,y)
local body={}
body.x=x
body.y=y
return body
end

function collide(C)
if C.state==BEGAN then
sound(SOUND_HIT, 3176)
end
end





-- 首先是控件TextButton
-- Ver 1.2
-- Author:bk194232658
TextButton = class()
function TextButton:init(x,y,capital)
self.x = x
self.y = y
self.backColor = color(255, 255, 255, 0)
self.textColor = color(255, 255, 255, 255)
self.pressedBackColor = color(255, 255, 255, 0)
self.pressedTextColor = color(0, 0, 0, 100)
self.unEnabledBackColor = color(255, 255, 255, 0)
self.unEnabledTextColor = color(67, 67, 67, 100)
self.font = nil
self.fontSize = 40
self.visible = true
self.enabled = true
self.clicked = nil
self.posMode = CENTER
-- self.alignment = UIConst.Alignment.Middle
self.pressedOffset = 1
self.state = false
self.touchid = 0
self.clip = true
self:setCapital(capital)
end
function TextButton:draw()
if self.visible==false then return end
pushStyle()
if self.clip then
local x,y = self.x,self.y
if self.posMode == CENTER then
x,y = x - self.width / 2,y - self.height / 2
end
clip(x,y,self.width,self.height)
end
noStroke()
textMode(CENTER)
if self.font then
font(self.font)
else
font("HelveticaNeue-Light")
end
fontSize(self.fontSize)
textWrapWidth(0)
textMode(CENTER)
if self.posMode == CENTER then
rectMode(CENTER)
else
rectMode(CORNER)
end

if self.state == false then
if self.enabled == true then
fill(self.backColor)
else
fill(self.unEnabledBackColor)
end
rect(self.x,self.y,self.width,self.height)
if self.enabled == true then
fill(self.textColor)
else
fill(self.unEnabledTextColor)
end
if self.posMode == CENTER then
text(self.capital,self.x,self.y)
else
text(self.capital,self.x + self.width / 2,self.y + self.height / 2)
end
else
fill(self.pressedBackColor)
rect(self.x,self.y,self.width,self.height) 
fill(self.pressedTextColor)
if self.posMode == CENTER then
text(self.capital,self.x + self.pressedOffset,self.y - self.pressedOffset)
else
text(self.capital,self.x + self.width / 2 + self.pressedOffset,self.y + self.height/2 - self.pressedOffset) 
end
end
clip()
popStyle()
end
function TextButton:touched(touch)
if self.visible == false or self.enabled == false then return end
local x,y,width,height = self.x,self.y,self.width,self.height
if self.posMode == CENTER then
x,y = x - width / 2,y - height / 2
end
if touch.x >= x and touch.y >= y and 
touch.x <= x + width and touch.y <= y + height then 
if touch.state == BEGAN then
self.state = true
self.touchid = touch.id
elseif touch.state == ENDED then
if self.clicked then self.clicked() end
self.state = false
self.touchid = 0
end
return true
elseif touch.state == ENDED and self.touchid == touch.id then
self.state = false
self.touchid = 0
end
return false
end
function TextButton:setCapital(capital)
self.capital = capital or ""
self:autoSize()
end
function TextButton:autoSize()
fontSize(self.fontSize)
self.width,self.height = textSize(self.capital)
self.width,self.height = self.width + 20,self.height + 10
end
function TextButton:setFontSize(size)
self.fontSize = size
self:autoSize()
end

StartMenu = class()

function StartMenu:init()
-- you can accept and set parameters here
self.One_Player=TextButton(WIDTH/2,HEIGHT/2,"单人游戏(Easy)")
self.One_Player.clicked=function()
Cc=3
Hard=3
OnePlayer:setup()
self.FootBall:destroy()
self.Edge:destroy()
self.Edge2:destroy()
end
self.One_Player_Middle=TextButton(WIDTH/2,HEIGHT/3*2,"单人游戏(Middle)")
self.One_Player_Middle.clicked=function()
Cc=3
Hard=6
OnePlayer:setup()
self.FootBall:destroy()
self.Edge:destroy()
self.Edge2:destroy()
end
self.One_Player_Hard=TextButton(WIDTH/2,HEIGHT/3,"单人游戏(Hard)")
self.One_Player_Hard.clicked=function()
Cc=3
Hard=10
OnePlayer:setup()
self.FootBall:destroy()
self.Edge:destroy()
self.Edge2:destroy()
end
self.Two_Player=TextButton(WIDTH/2,HEIGHT/5,"双人游戏")
self.Two_Player.clicked=function()
Cc=2
TwoPlayer:setup()
self.FootBall:destroy()
self.Edge:destroy()
self.Edge2:destroy()
end
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.Edge=physics.body(CHAIN,loop,vec2(0,0),vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
self.Edge2=physics.body(CHAIN,loop,vec2(0,0),vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
end

function StartMenu:draw()
-- Codea does not automatically call this method
fill(255, 255, 255, 255)
physics.gravity(Gravity*5)
ellipse(self.FootBall.x,self.FootBall.y,40)
self.One_Player:draw()
self.Two_Player:draw()
self.One_Player_Middle:draw()
self.One_Player_Hard:draw()
pushStyle()
fill(255, 255, 255, 255)
font("AmericanTypewriter-Bold")
fontSize(70)
text("Scorer 2D",WIDTH/2,HEIGHT-200)
popStyle()
end

function StartMenu:touched(touch)
-- Codea does not automatically call this method
self.One_Player:touched(touch)
self.Two_Player:touched(touch)
self.One_Player_Middle:touched(touch)
self.One_Player_Hard:touched(touch)
end

OnePlayer = class()

function OnePlayer:init()
-- you can accept and set parameters here
self.Back=TextButton(50,HEIGHT-50,"<")
self.Back.clicked=function()
restart()
end
end

function OnePlayer:setup()
self.P1=0
self.AIMoveSpeed=Hard
self.P2=0
self.Edge=physics.body(CHAIN,loop,vec2(0,0),vec2(WIDTH/2-180,0),vec2(WIDTH/2-180,-100),vec2(WIDTH/2+180,-100),vec2(WIDTH/2+180,0),vec2(WIDTH,0),vec2(WIDTH,HEIGHT,vec2(WIDTH/2+180,HEIGHT),vec2(WIDTH/2+180,HEIGHT+100),vec2(WIDTH/2-180,HEIGHT+100),vec2(WIDTH/2-180,HEIGHT),vec2(0,HEIGHT),vec2(0,0)))
self.Edge2=physics.body(CHAIN,loop,vec2(0,0),vec2(0,HEIGHT),vec2(WIDTH/2-180,HEIGHT),vec2(WIDTH/2-180,HEIGHT+100),vec2(WIDTH/2+180,HEIGHT+100),vec2(WIDTH/2+180,HEIGHT),vec2(WIDTH,HEIGHT))
physics.gravity(0,0)
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.FootBall:applyForce(vec2(0,-100))
self.Player1={}
self.Player1Back={}
self.Player2={}
self.Player2Back={}
for i=1,3 do
table.insert(self.Player1,CreatPlayer(i*WIDTH/4,HEIGHT/2+70))
table.insert(self.Player1,CreatPlayer(i*WIDTH/4,HEIGHT/2-210))
table.insert(self.Player2,CreatPlayer(i*WIDTH/4,HEIGHT/2-70))
table.insert(self.Player2,CreatPlayer(i*WIDTH/4,HEIGHT/2+210))
end
for i=1,2 do
table.insert(self.Player1Back,CreatPlayer(i*WIDTH/3,HEIGHT/2-210-140))
table.insert(self.Player2Back,CreatPlayer(i*WIDTH/3,HEIGHT/2+210+140))
end
end

function OnePlayer:draw()
-- Codea does not automatically call this method
if math.abs(self.FootBall.linearVelocity.y)<10 then
self.FootBall:applyForce(vec2(0,HEIGHT/2-self.FootBall.y))
end
for k,v in pairs(self.Player2) do
if self.FootBall.x-self.Player2[4].x>20 then
v.x=v.x+self.AIMoveSpeed
elseif self.FootBall.x-self.Player2[4].x<-20 then
v.x=v.x-self.AIMoveSpeed
end
end
for k,v in pairs(self.Player2Back) do
if self.FootBall.x-self.Player2Back[2].x>20 then
v.x=v.x+self.AIMoveSpeed*1.3
elseif self.FootBall.x-self.Player2Back[2].x<-20 then
v.x=v.x-self.AIMoveSpeed*1.3
end
end
physics.continuous=true
if self.FootBall.y>HEIGHT then
self.P1 = self.P1 + 1
self.FootBall:destroy()
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.FootBall:applyForce(vec2(0,-100))
elseif self.FootBall.y<0 then
self.P2 = self.P2 + 1
self.FootBall:destroy()
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.FootBall:applyForce(vec2(0,100))
end
stroke(218, 218, 218, 255)
fill(218, 218, 218, 0)
ellipse(WIDTH/2,HEIGHT/2,200)
line(0,HEIGHT/2,WIDTH,HEIGHT/2)
rect(WIDTH/2,HEIGHT,250,200)
rect(WIDTH/2,0,250,200)
stroke(255, 255, 255, 255)
fill(255, 255, 255, 255)
for k,v in pairs(self.Player1) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
for k,v in pairs(self.Player1Back) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
for k,v in pairs(self.Player2) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
for k,v in pairs(self.Player2Back) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
pushStyle()
fill(31, 78, 37, 255)
stroke(31, 78, 37, 255)
strokeWidth(5)
line(0,HEIGHT/2+70-5,WIDTH,HEIGHT/2+70-5)
line(0,HEIGHT/2-70-5,WIDTH,HEIGHT/2-70-5)
line(0,HEIGHT/2+210-5,WIDTH,HEIGHT/2+210-5)
line(0,HEIGHT/2-210-5,WIDTH,HEIGHT/2-210-5)
line(0,HEIGHT/2+350-5,WIDTH,HEIGHT/2+350-5)
line(0,HEIGHT/2-350-5,WIDTH,HEIGHT/2-350-5)
ellipse(self.FootBall.x-5,self.FootBall.y-5,40)
popStyle()
ellipse(self.FootBall.x,self.FootBall.y,40)
line(0,HEIGHT/2+70,WIDTH,HEIGHT/2+70)
line(0,HEIGHT/2-70,WIDTH,HEIGHT/2-70)
line(0,HEIGHT/2+210,WIDTH,HEIGHT/2+210)
line(0,HEIGHT/2-210,WIDTH,HEIGHT/2-210)
line(0,HEIGHT/2+350,WIDTH,HEIGHT/2+350)
line(0,HEIGHT/2-350,WIDTH,HEIGHT/2-350)
line(WIDTH/2-180,0,WIDTH/2+180,0)
line(WIDTH/2-180,HEIGHT,WIDTH/2+180,HEIGHT)
-- Codea does not automatically call this method
fill(27, 58, 145, 255)
for k,v in pairs(self.Player1) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y+60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,2000))
sound(SOUND_HIT, 3176)
end
end
for k,v in pairs(self.Player1Back) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y+60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,2000))
sound(SOUND_HIT, 3176)
end
end
fill(122, 30, 30, 255)
for k,v in pairs(self.Player2) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y-60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,-2000))
sound(SOUND_HIT, 3176)
end
end
for k,v in pairs(self.Player2Back) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y-60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,-2000))
sound(SOUND_HIT, 3176)
end
end
fill(255, 255, 255, 255)
text(self.P1,WIDTH/2,50)
text(self.P2,WIDTH/2,HEIGHT-50)
self.Back:draw()
end

function OnePlayer:touched(touch)
-- Codea does not automatically call this method
if touch.state==BEGAN or MOVING then
for k,v in pairs(self.Player1) do
if math.abs(touch.deltaX)<20 then
v.x=v.x+touch.deltaX
elseif touch.deltaX<0 then
v.x=v.x-20
elseif touch.deltaX>0 then
v.x=v.x+20
end
end
for k,v in pairs(self.Player1Back) do
if math.abs(touch.deltaX)<20 then
v.x=v.x+touch.deltaX*1.3
elseif touch.deltaX<0 then
v.x=v.x-20*1.3
elseif touch.deltaX>0 then
v.x=v.x+20*1.3
end
end
end
self.Back:touched(touch)
end

TwoPlayer = class()

function TwoPlayer:init()
-- you can accept and set parameters here
self.Back=TextButton(50,HEIGHT-50,"<")
self.Back.clicked=function()
restart()
end
end

function TwoPlayer:setup()
self.P1=0
self.P2=0
self.Edge=physics.body(CHAIN,loop,vec2(0,0),vec2(WIDTH/2-180,0),vec2(WIDTH/2-180,-100),vec2(WIDTH/2+180,-100),vec2(WIDTH/2+180,0),vec2(WIDTH,0),vec2(WIDTH,HEIGHT,vec2(WIDTH/2+180,HEIGHT),vec2(WIDTH/2+180,HEIGHT+100),vec2(WIDTH/2-180,HEIGHT+100),vec2(WIDTH/2-180,HEIGHT),vec2(0,HEIGHT),vec2(0,0)))
self.Edge2=physics.body(CHAIN,loop,vec2(0,0),vec2(0,HEIGHT),vec2(WIDTH/2-180,HEIGHT),vec2(WIDTH/2-180,HEIGHT+100),vec2(WIDTH/2+180,HEIGHT+100),vec2(WIDTH/2+180,HEIGHT),vec2(WIDTH,HEIGHT))
physics.gravity(0,0)
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.FootBall:applyForce(vec2(0,-100))
self.Player1={}
self.Player1Back={}
self.Player2={}
self.Player2Back={}
for i=1,3 do
table.insert(self.Player1,CreatPlayer(i*WIDTH/4,HEIGHT/2+70))
table.insert(self.Player1,CreatPlayer(i*WIDTH/4,HEIGHT/2-210))
table.insert(self.Player2,CreatPlayer(i*WIDTH/4,HEIGHT/2-70))
table.insert(self.Player2,CreatPlayer(i*WIDTH/4,HEIGHT/2+210))
end
for i=1,2 do
table.insert(self.Player1Back,CreatPlayer(i*WIDTH/3,HEIGHT/2-210-140))
table.insert(self.Player2Back,CreatPlayer(i*WIDTH/3,HEIGHT/2+210+140))
end
end

function TwoPlayer:draw()
if math.abs(self.FootBall.linearVelocity.y)<10 then
self.FootBall:applyForce(vec2(0,HEIGHT/2-self.FootBall.y))
end
physics.continuous=true
if self.FootBall.y>HEIGHT then
self.P1 = self.P1 + 1
self.FootBall:destroy()
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.FootBall:applyForce(vec2(0,-100))
elseif self.FootBall.y<0 then
self.P2 = self.P2 + 1
self.FootBall:destroy()
self.FootBall=CreatBall(WIDTH/2,HEIGHT/2,20)
self.FootBall.bullet=true
self.FootBall.restitution=0.2
self.FootBall:applyForce(vec2(0,100))
end
stroke(218, 218, 218, 255)
fill(218, 218, 218, 0)
ellipse(WIDTH/2,HEIGHT/2,200)
line(0,HEIGHT/2,WIDTH,HEIGHT/2)
rect(WIDTH/2,HEIGHT,250,200)
rect(WIDTH/2,0,250,200)
stroke(255, 255, 255, 255)
fill(255, 255, 255, 255)
for k,v in pairs(self.Player1) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
for k,v in pairs(self.Player1Back) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
for k,v in pairs(self.Player2) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
for k,v in pairs(self.Player2Back) do
pushStyle()
fill(31, 78, 37, 255)
strokeWidth(0)
ellipse(v.x-5,v.y-5,60)
popStyle()
end
pushStyle()
fill(31, 78, 37, 255)
stroke(31, 78, 37, 255)
strokeWidth(5)
line(0,HEIGHT/2+70-5,WIDTH,HEIGHT/2+70-5)
line(0,HEIGHT/2-70-5,WIDTH,HEIGHT/2-70-5)
line(0,HEIGHT/2+210-5,WIDTH,HEIGHT/2+210-5)
line(0,HEIGHT/2-210-5,WIDTH,HEIGHT/2-210-5)
line(0,HEIGHT/2+350-5,WIDTH,HEIGHT/2+350-5)
line(0,HEIGHT/2-350-5,WIDTH,HEIGHT/2-350-5)
ellipse(self.FootBall.x-5,self.FootBall.y-5,40)
popStyle()
ellipse(self.FootBall.x,self.FootBall.y,40)
line(0,HEIGHT/2+70,WIDTH,HEIGHT/2+70)
line(0,HEIGHT/2-70,WIDTH,HEIGHT/2-70)
line(0,HEIGHT/2+210,WIDTH,HEIGHT/2+210)
line(0,HEIGHT/2-210,WIDTH,HEIGHT/2-210)
line(0,HEIGHT/2+350,WIDTH,HEIGHT/2+350)
line(0,HEIGHT/2-350,WIDTH,HEIGHT/2-350)
line(WIDTH/2-180,0,WIDTH/2+180,0)
line(WIDTH/2-180,HEIGHT,WIDTH/2+180,HEIGHT)
-- Codea does not automatically call this method
fill(27, 58, 145, 255)
for k,v in pairs(self.Player1) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y+60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,2000))
sound(SOUND_HIT, 3176)
end
end
for k,v in pairs(self.Player1Back) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y+60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,2000))
sound(SOUND_HIT, 3176)
end
end
fill(122, 30, 30, 255)
for k,v in pairs(self.Player2) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y-60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,-2000))
sound(SOUND_HIT, 3176)
end
end
for k,v in pairs(self.Player2Back) do
ellipse(v.x,v.y,60,60)
if vec2(self.FootBall.x,self.FootBall.y):dist(vec2(v.x,v.y))<30 then
self.FootBall.y=self.FootBall.y-60
self.FootBall:applyForce(vec2((self.FootBall.x-v.x)*200,-2000))
sound(SOUND_HIT, 3176)
end
end
fill(255, 255, 255, 255)
text(self.P1,WIDTH/2,50)
text(self.P2,WIDTH/2,HEIGHT-50)
self.Back:draw()
end

function TwoPlayer:touched(touch)
-- Codea does not automatically call this method
if touch.state==BEGAN or MOVING then
if touch.y<=WIDTH/2 then
for k,v in pairs(self.Player1) do
if math.abs(touch.deltaX)<20 then
v.x=v.x+touch.deltaX
elseif touch.deltaX<0 then
v.x=v.x-20
elseif touch.deltaX>0 then
v.x=v.x+20
end
end
for k,v in pairs(self.Player1Back) do
if math.abs(touch.deltaX)<20 then
v.x=v.x+touch.deltaX*1.3
elseif touch.deltaX<0 then
v.x=v.x-20*1.3
elseif touch.deltaX>0 then
v.x=v.x+20*1.3
end
end
else
for k,v in pairs(self.Player2) do
if math.abs(touch.deltaX)<20 then
v.x=v.x+touch.deltaX
elseif touch.deltaX<0 then
v.x=v.x-20
elseif touch.deltaX>0 then
v.x=v.x+20
end
end
for k,v in pairs(self.Player2Back) do
if math.abs(touch.deltaX)<20 then
v.x=v.x+touch.deltaX*1.3
elseif touch.deltaX<0 then
v.x=v.x-20*1.3
elseif touch.deltaX>0 then
v.x=v.x+20*1.3
end
end
end
end
self.Back:touched(touch)
end
